package zjhr.com.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.util.List;

import javax.activation.MimetypesFileTypeMap;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.MimeType;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import zjhr.com.pojo.EasyUIPageQuery;
import zjhr.com.pojo.ZjOutmoney;
import zjhr.com.service.OutMoneyService;
import zjhr.com.utils.FileUtils;
import zjhr.com.utils.JsonUtils;

@Controller
public class OutMoneyController {
	@Autowired
	private OutMoneyService outMoneyService; 
	
	//添加进账信息
	@RequestMapping(value="/addOutMoneyInfo.action", method=RequestMethod.POST)
	public String addInMoney(ZjOutmoney outmoney){
		outMoneyService.save(outmoney);
		return "/caiwu/outMoney.jsp";
	}
	
	//修改进账信息
	@RequestMapping(value="/editOutMoneyInfo.action", method=RequestMethod.POST)
	public String editInMoney(ZjOutmoney outmoney){
		outMoneyService.edit(outmoney);
		
		return "/caiwu/outMoney.jsp";
	}
	//分页
	@RequestMapping("/outMoneyPageQuery.action")
	@ResponseBody
	public String outMoneyPageQuery(Integer page,Integer rows){
		EasyUIPageQuery pageQuery = outMoneyService.pageQuery(page, rows);
		return JsonUtils.objectToJson(pageQuery);
	}
	
	//导出进账信息
		@RequestMapping(value="/exportOutMoneyInfo.action",method=RequestMethod.POST)
		public void outMoneyExport(String beginDate,String endDate,HttpServletResponse response,HttpServletRequest request) throws Exception{
			List<ZjOutmoney> list = outMoneyService.selectAll(beginDate,endDate);
		
			HSSFWorkbook workBook = new HSSFWorkbook();
			HSSFSheet sheet = workBook.createSheet("出账记录");
			HSSFRow headRow = sheet.createRow(0);
			headRow.createCell(0).setCellValue("编号");
			headRow.createCell(1).setCellValue("出账人");
			headRow.createCell(2).setCellValue("出账时间");
			headRow.createCell(3).setCellValue("出账金额");
			headRow.createCell(4).setCellValue("出账内容");
			for (ZjOutmoney outmoney : list) {
				HSSFRow hssfRow = sheet.createRow(sheet.getLastRowNum()+1);
				hssfRow.createCell(0).setCellValue(sheet.getLastRowNum());
				hssfRow.createCell(1).setCellValue(outmoney.getOutname());
				hssfRow.createCell(2).setCellValue(outmoney.getOutdate());
				hssfRow.createCell(3).setCellValue(outmoney.getOutmoney());
				hssfRow.createCell(4).setCellValue(outmoney.getOutcomment());
			}
			//第三步：使用输出流进行文件下载（一个流、两个头）
			String filename = "出账数据.xls";
			
			String contentType = new MimetypesFileTypeMap().getContentType(filename);//获取文件类型
			ServletOutputStream out = response.getOutputStream();
			response.setContentType(contentType);
			
			//获取客户端浏览器类型
			String agent = request.getHeader("User-Agent");
			filename = FileUtils.encodeDownloadFilename(filename, agent);
			response.setHeader("content-disposition", "attachment;filename="+filename);
			workBook.write(out);
			out.close();
		}

}
